package com.eveinfo.display.asset;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import android.content.Context;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.api.asset.ApiAsset;
import com.eveinfo.sql.data.Item;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.Tools;

public class DroneAdapter extends BaseAdapter {

	private Context context;
	private GestureDetector gestureDetector;
	
	private Row[] rows;

	class Point {
		float x;
		float y;
	}

	class Row {
		Item item;
		long quantity;
	}

	private Point point = new Point();

	public DroneAdapter(Context context, int textViewRessourceID, ListView listView, ArrayList<ApiAsset> assets,
			GestureDetector gestureDetector) {
		this.context = context;
		HashMap<Integer, Row> rows = new HashMap<Integer, Row>();
		final int size = assets.size();
		for (int i = 0; i < size; i++) {
			ApiAsset asset = assets.get(i);
			final int typeID = asset.getTypeID();
			Row row = rows.get(typeID);
			if (row == null) {
				row = new Row();
				row.item = asset.getItem();
				row.quantity = 0;
			}
			row.quantity += asset.getQuantity();
			rows.put(typeID, row);
		}
		final Collection<Row> values = rows.values();
		this.rows = new Row[values.size()];
		this.rows = values.toArray(this.rows);
		this.gestureDetector = gestureDetector;
		listView.setAdapter(this);
	}

	@Override
	public int getCount() {
		return rows.length;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.itemr2, null);
		}

		final Row row = rows[position];
		final Item item = row.item;

		ImageView imageIV = (ImageView) view.findViewById(R.id.IVItemR2Picture);
		imageIV.setImageDrawable(Tools.getItemPicture(item, EveInfo.getPreferences().getAssetsImageSize()));

		TextView nameTV = (TextView) view.findViewById(R.id.TVItemR2Name);
		nameTV.setText(item.getTypeName());

		TextView numberTV = (TextView) view.findViewById(R.id.TVItemR2Number);
		numberTV.setText("" + row.quantity);
		
		view.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (gestureDetector.onTouchEvent(event)) {
					return true;
				}
				final int action = event.getAction();
				final float x = event.getRawX();
				final float y = event.getRawY();
				if (action == MotionEvent.ACTION_UP && point.x == x && point.y == y) {
					ActivityLauncher.displayItem(context, item.getTypeID());
					return true;
				} else if (action == MotionEvent.ACTION_DOWN) {
					point.x = x;
					point.y = y;
					return false;
				}
				return false;
			}
		});
		view.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				return true;
			}
		});
		return view;
	}

}
